//
//  GQJPatternView.m
//  TotalDemo
//
//  Created by gujian on 16/7/5.
//  Copyright © 2016年 gujian. All rights reserved.
//

#import "GQJPatternView.h"

#define H_PATTERN_SIZE 16
#define V_PATTERN_SIZE 18

#define PSIZE 16

@implementation GQJPatternView


- (void)drawRect:(CGRect)rect {
    // Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext();
//    MyColoredPatternPainting(context, CGRectMake(0, 0, self.width, self.height));
    MyStencilPatternPainting(context,CGRectMake(0, 0, self.width, self.height));
}


void MyDrawColoredPattern (void *info, CGContextRef myContext)
{
    CGFloat subunit = 5;
    
    CGRect myRect1 = {{0,0},{subunit, subunit}},
    myRect2 = {{subunit,subunit}, {subunit,subunit}},
    myRect3 = {{0,subunit},subunit,subunit},
    myRect4 = {{subunit,0},{subunit,subunit}};
    
    CGContextSetRGBFillColor(myContext, 0, 0, 1, 0.5);
    CGContextFillRect(myContext, myRect1);
    
    CGContextSetRGBFillColor(myContext, 1, 0, 0, 0.5);
    CGContextFillRect(myContext, myRect2);
    
    CGContextSetRGBFillColor(myContext, 0, 1, 0, 0.5);
    CGContextFillRect(myContext, myRect3);
    
    CGContextSetRGBFillColor(myContext, .5, 0, .5, 0.5);
    CGContextFillRect(myContext, myRect4);
    
    
}

void MyColoredPatternPainting(CGContextRef mycontext, CGRect rect)
{
    CGPatternRef pattern;
    CGColorSpaceRef patternSpace;
    CGFloat alpha = 1;
    
    static const CGPatternCallbacks callbacks = {0,&MyDrawColoredPattern,NULL};
    
    CGContextSaveGState(mycontext);
    patternSpace = CGColorSpaceCreatePattern(NULL);
    CGContextSetFillColorSpace(mycontext, patternSpace);
    CGColorSpaceRelease(patternSpace);
    
    pattern = CGPatternCreate(NULL, CGRectMake(0, 0, H_PATTERN_SIZE, V_PATTERN_SIZE), CGAffineTransformMake(1, 0, 0, 1, 0, 0), H_PATTERN_SIZE, V_PATTERN_SIZE, kCGPatternTilingConstantSpacing, true, &callbacks);
    
    CGContextSetFillPattern(mycontext, pattern, &alpha);
    CGPatternRelease(pattern);
    CGContextFillRect(mycontext, rect);
    CGContextRestoreGState(mycontext);
    
}

void MyStencilPatternPainting(CGContextRef context, CGRect windowRect)
{
    CGPatternRef pattern;
    CGColorSpaceRef baseSpace;
    CGColorSpaceRef patternSapce;
    static const CGFloat color[4] = {0,1,0,1};
    static const CGPatternCallbacks callbacks = {0, &drawStar, NULL};
    
    baseSpace = CGColorSpaceCreateDeviceRGB();
    patternSapce = CGColorSpaceCreatePattern(baseSpace);
    CGContextSetFillColorSpace(context, patternSapce);
    CGColorSpaceRelease(patternSapce);
    CGColorSpaceRelease(baseSpace);
    pattern = CGPatternCreate(NULL, CGRectMake(0, 0, PSIZE, PSIZE), CGAffineTransformIdentity, PSIZE, PSIZE, kCGPatternTilingConstantSpacing, false, &callbacks);
    CGContextSetFillPattern(context, pattern, color);
    CGPatternRelease(pattern);
    CGContextFillRect(context, CGRectMake(0,0, PSIZE*20, PSIZE*20));
}

static void drawStar(void *info , CGContextRef context)
{
    int k;
    double r, theta;
    
    r = 0.8 * PSIZE / 2.0;
    theta = 2 * M_PI * (2.0/5.0);  //144degrees
    
    CGContextTranslateCTM(context, PSIZE/2.0, PSIZE/2.0);
    
    [[UIColor yellowColor] set];
    CGContextSetLineWidth(context, 1.0);
    
    CGContextMoveToPoint(context, 0, r);
    
    for (k = 1; k < 5; k++) {
        CGContextAddLineToPoint(context, r * sin(k * theta), r * cos(k * theta));
    }
    
    
    CGContextClosePath(context);
    
    [[UIColor redColor] set];
    
    
    CGContextFillPath(context);
    
}


@end
